package leetcode_801_900;

public class LeeCode_831 {
    public static void main(String[] args) {
        System.out.println(maskPII("LeetCode@LeetCode.com"));
        System.out.println(maskPII("AB@qq.com"));
        System.out.println(maskPII("1(234)567-890"));
        System.out.println(maskPII("86-(10)12345678"));
    }
    private static String maskPII(String s) {
        int len = s.length();
        StringBuilder sb = new StringBuilder();
        if (s.contains("@")) {
            int m = s.indexOf("@");
            sb.append(s.charAt(0) >= 'A' && s.charAt(0) <= 'Z' ? (char) (s.charAt(0) - 'A' + 'a') : s.charAt(0));
            sb.append("*****");
            sb.append(s.charAt(m - 1) >= 'A' && s.charAt(m - 1) <= 'Z' ?
                    (char) (s.charAt(m - 1) - 'A' + 'a') : s.charAt(m - 1));
            sb.append('@');
            for (int i = m + 1; i < len; i++) {
                sb.append(s.charAt(i) >= 'A' && s.charAt(i) <= 'Z' ?
                        (char) (s.charAt(i) - 'A' + 'a') : s.charAt(i));
            }
        }else {
            String nums = "";
            for (int i = 0; i < len; i++) {
                if (s.charAt(i) >= '0' && s.charAt(i) <= '9')
                    nums += s.charAt(i);
            }
            String[] pre = new String[]{"***-***-","+*-***-***-","+**-***-***-","+***-***-***-"};
            sb.append(pre[nums.length() - 10]);
            sb.append(nums.substring(nums.length() - 4));
        }
        return sb.toString();
    }
}
